//
//  NXfirststepViewController.m
//  BiologyLibrary
//
//  Created by milo on 16/7/2.
//  Copyright © 2016年 youxh. All rights reserved.
//

#import "NXfirststepViewController.h"
#import "NXSecondStepViewController.h"
#import "MamberselectViewController.h"
#import "XYsecondStepViewController.h"
#import "XJSecondStepViewController.h"
#import "XQSecondStepViewController.h"
#import "XJChouxueViewController.h"
#import "ServiceUser.h"
@interface NXfirststepViewController ()

@end

@implementation NXfirststepViewController

static const NSString * KEY_STEP  = @"step1";


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"setp1 record === %@", self.NewRecordArray.firstObject.getDetailDict);
    [self setNaviBarTitle:self.NewRecordArray.firstObject.name];
    
    _maintableview=[[UITableView alloc]initWithFrame:CGRectMake(10, 64, fWidth_Current-20, fHeight_Current-64-50)];
    _maintableview.backgroundColor = [UIColor clearColor];
    _maintableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _maintableview.dataSource = self;
    _maintableview.delegate = self;
    [self.view addSubview:_maintableview];
    
    userarr=[[NSMutableArray alloc] init];
    useridarr=[[NSMutableArray alloc] init];

    if ([[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] &&
        [[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] count] > 0 &&
        [[[[self.NewRecordArray.firstObject getDetailDict] objectForKey:KEY_STEP] objectAtIndex:0] objectForKey:KEY_RECORD_ANSWER]){
        
        [userarr addObject:[[[self.NewRecordArray.firstObject.getDetailDict objectForKey:KEY_STEP] objectAtIndex:0] objectForKey:KEY_RECORD_ANSWER]];
        [useridarr addObject:[[[self.NewRecordArray.firstObject.getDetailDict objectForKey:KEY_STEP] objectAtIndex:1] objectForKey:KEY_RECORD_ANSWER]];

    }
    
    UIImageView*buttomview=[[UIImageView alloc]initWithFrame:CGRectMake(0, fHeight_Current-50, fWidth_Current, 50)];
    buttomview.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
    [self.view addSubview:buttomview];
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, fWidth_Current, 50)];
    label.backgroundColor=[UIColor clearColor];
    label.textAlignment=1;
    label.font=[UIFont systemFontOfSize:14];
    label.textColor=[UIColor lightGrayColor];
    label.text= [NSString stringWithFormat:@"当下是第%ld/%ld步",
                 (long)self.NewRecordArray.firstObject.savedStepCount + 1, (long)self.NewRecordArray.firstObject.allStepCount];
    [buttomview addSubview:label];
}

#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 2;

    }else{
        return 1;

    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;//返回标题数组中元素的个数来确定分区的个数
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            return 60;
        }else{
            if (userarr.count == 0) {
                return (18+25)+18;
            }else{
                return (((userarr.count-1)/4)+1)*(18+25)+18;

            }
        }
    }else{
        return 50;
        
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView*headview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, fWidth_Current, 18)];
    headview.backgroundColor=[UIColor clearColor];
    return headview;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *strIdentifier = @"strIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];
    
    
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                      reuseIdentifier:strIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
        
    
    switch (indexPath.section) {
        case 0:
            if (indexPath.row==0) {
                UILabel *titlelab=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, fWidth_Current-40, 60)];
                titlelab.textColor=[UIColor whiteColor];
                titlelab.backgroundColor=[UIColor clearColor];
                titlelab.text=@"1、操作者";
                UIImageView*lineview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 60, fWidth_Current-20, 1)];
                lineview.backgroundColor=[UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:0.3];
                [cell addSubview:titlelab];
                [cell addSubview:lineview];
            }else{
//                UIButton *addbtn=[[UIButton alloc]initWithFrame:CGRectMake(10, 15, 30, 30)];
//                addbtn.backgroundColor=[UIColor clearColor];
//                [addbtn setImage:[UIImage imageNamed:@"useraddbtn"] forState:UIControlStateNormal];
//                [addbtn addTarget:self action:@selector(addpeople) forControlEvents:UIControlEventTouchUpInside];
//                [cell addSubview:addbtn];
//                float juli=(fWidth_Current-20-50-4*55-10)/3;
//                for (int i=0; i<userarr.count; i++) {
//                    UIButton *namebtn=[[UIButton alloc]initWithFrame:CGRectMake(50+i%4*(55+juli), i/4*(25+18)+18, 55, 25)];
//                    namebtn.backgroundColor=[UIColor clearColor];
//                    [namebtn setTitle:[userarr objectAtIndex:i] forState:UIControlStateNormal];
//                    namebtn.titleLabel.font=[UIFont systemFontOfSize:12];
//                    
//                    [namebtn setTitleColor:[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0] forState:UIControlStateNormal];
//                    namebtn.layer.borderWidth = 1;
//                    namebtn.layer.cornerRadius=11;
//                    namebtn.clipsToBounds=YES;
//                    namebtn.layer.borderColor = [[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0] CGColor];
//                    [cell addSubview:namebtn];
//                }
//                if (self.fromtype && [self.fromtype isEqualToString:@"search"]) {
//                    addbtn.hidden = YES;
//                }else{
//                    addbtn.hidden = NO;
//                }
                UILabel *namelab=[[UILabel alloc]initWithFrame:CGRectMake(20, 15, 120, 30)];
                namelab.backgroundColor=[UIColor clearColor];
                namelab.textColor=[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
                namelab.font=[UIFont systemFontOfSize:18];
                namelab.text=[APPUserDict objectForKey:@"displayname"];
                [cell addSubview:namelab];
                
            }
            break;
        case 1:{
            cell.backgroundColor=[UIColor clearColor];
            
            if (self.fromtype && [self.fromtype isEqualToString:@"search"]) {
                
                UIButton *btn2=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
                btn2.backgroundColor=[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
                btn2.layer.cornerRadius=5;
                [btn2 setTitle:@"下一步" forState:UIControlStateNormal];
                btn2.clipsToBounds=YES;
                [btn2 addTarget:self action:@selector(nextStepClick) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:btn2];
            }else{
            
                UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, (fWidth_Current-30)/2, 50)];
                btn1.backgroundColor=[UIColor colorWithRed:0.16 green:0.19 blue:0.24 alpha:1.0];
                btn1.layer.cornerRadius=5;
                [btn1 setTitle:@"取消" forState:UIControlStateNormal];
                btn1.clipsToBounds=YES;
                [btn1 addTarget:self action:@selector(cancelclick) forControlEvents:UIControlEventTouchUpInside];
                
                [cell addSubview:btn1];
                UIButton *btn2=[[UIButton alloc]initWithFrame:CGRectMake((fWidth_Current-30)/2+10, 0, (fWidth_Current-30)/2, 50)];
                btn2.backgroundColor=[UIColor colorWithRed:0.22 green:0.57 blue:0.91 alpha:1.0];
                btn2.layer.cornerRadius=5;
                [btn2 setTitle:@"保存" forState:UIControlStateNormal];
                btn2.clipsToBounds=YES;
                [btn2 addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:btn2];
            }
        }
            break;
            
        default:
            break;
    }
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)addpeople{
    MamberselectViewController *MamberselectVC= [[MamberselectViewController alloc] init];
    MamberselectVC.delegate=self;
    MamberselectVC.nowIndex=0;
    [self presentViewController:MamberselectVC animated:YES completion:nil];
}
#pragma mark - MenberselectDelegate
-(void)Manberselectfinished:(MamberselectViewController *)MamberselectViewController withmenberarr:(NSMutableArray *)menberarr withIndex:(NSInteger)index{
    for (int i=0; i<menberarr.count; i++) {
        ServiceUser *tempUser = (ServiceUser *)[menberarr objectAtIndex:i];

        NSString *name1=tempUser.displayname;
        NSString *userid=tempUser.userid;
        [userarr removeAllObjects];
        [useridarr removeAllObjects];

        if (![userarr containsObject:name1]) {
            [userarr addObject:name1];
            [useridarr addObject:userid];
        }
    }
    [_maintableview reloadData];
}
-(void)cancelclick{
    UIViewController*miloViewControl=(UIViewController *)[[self.navigationController viewControllers]objectAtIndex:1];
    [self.navigationController popToViewController:miloViewControl animated:YES];
}
-(void)saveClick{
   
    [self creatDetailDict];
    if (self.NewRecordArray.firstObject.type==1) {
        XYsecondStepViewController *secondStepVC = [[XYsecondStepViewController alloc] init];
        secondStepVC.NewRecordArray=self.NewRecordArray;
        [self.navigationController pushViewController:secondStepVC animated:YES];
    }else if (self.NewRecordArray.firstObject.type==4 || self.NewRecordArray.firstObject.type==5){
        XJChouxueViewController *secondStepVC = [[XJChouxueViewController alloc] init];
        secondStepVC.NewRecordArray=self.NewRecordArray;
        [self.navigationController pushViewController:secondStepVC animated:YES];
    }else{
        NXSecondStepViewController *secondStepVC = [[NXSecondStepViewController alloc] init];
        secondStepVC.NewRecordArray=self.NewRecordArray;
        [self.navigationController pushViewController:secondStepVC animated:YES];
    }

}

-(void)creatDetailDict{
    NSString*username=[APPUserDict objectForKey:@"displayname"];
    NSString*userID=[APPUserDict objectForKey:@"userid"];

    NSDictionary *questionDict = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"1、操作者", KEY_RECORD_QUESTION,
                                  TYPE_QUESTION_DEFAULT, KEY_RECORD_TYPE,
                                  username, KEY_RECORD_ANSWER, nil];
    NSDictionary *questionDict2 = [NSDictionary dictionaryWithObjectsAndKeys:
                                  @"操作者id", KEY_RECORD_QUESTION,
                                  TYPE_QUESTION_DEFAULT, KEY_RECORD_TYPE,
                                  userID, KEY_RECORD_ANSWER, nil];

    NSArray *qustionArr = [NSArray arrayWithObjects:questionDict,questionDict2, nil];
    for (Record *oneRecord in self.NewRecordArray) {
        NSMutableDictionary *tempDict = [oneRecord getDetailDict];
        [tempDict setObject:qustionArr forKey:KEY_STEP];
        [oneRecord setDetailDict:tempDict];
        NSLog(@"tempDict %@", tempDict);
        oneRecord.recordDetail = [tempDict JSONString];
        oneRecord.savedStepCount = 1;
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [oneRecord update];
        });
    }
    
   
}

-(void)nextStepClick{
    
    self.NewRecordArray.firstObject.savedStepCount = 1;

    if (self.NewRecordArray.firstObject.type==1) {
        XYsecondStepViewController *secondStepVC = [[XYsecondStepViewController alloc] init];
        secondStepVC.fromtype = @"search";
        secondStepVC.NewRecordArray=self.NewRecordArray;
        [self.navigationController pushViewController:secondStepVC animated:YES];
    }else if (self.NewRecordArray.firstObject.type==4 || self.NewRecordArray.firstObject.type==5){
        XJChouxueViewController *secondStepVC = [[XJChouxueViewController alloc] init];
        secondStepVC.fromtype = @"search";
        secondStepVC.NewRecordArray=self.NewRecordArray;
        [self.navigationController pushViewController:secondStepVC animated:YES];
    }else{
        NXSecondStepViewController *secondStepVC = [[NXSecondStepViewController alloc] init];
        secondStepVC.fromtype = @"search";
        secondStepVC.NewRecordArray=self.NewRecordArray;
        [self.navigationController pushViewController:secondStepVC animated:YES];
    }
}

@end
